﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCTrangThaiLamViec : System.Web.UI.UserControl
    {
        NSTrangThaiLamViec trangThai = new NSTrangThaiLamViec();
        private List<NSTrangThaiLamViec> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            cbbTrangThai.SelectedItem.Value = string.Empty;
            dtNgayBatDau.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtNoiDung.Text = string.Empty;
        }
        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {
            storeTrangThai.DataSource = common.LoadDataCommonByCodeKey(common.TrangThaiLamViec);
            storeTrangThai.DataBind();
            hdNhanVienId.Text = nhanvienID;
            hdTrangThaiId.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdTrangThaiLamViec.Title = "Tạo mới trạng thái";
                ResetControl();
                hdType.Text = type;
                btnSave.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdTrangThaiLamViec.Title = "Sửa đổi trạng thái";
                hdType.Text = type;
                btnSave.Hidden = false;
                LoadDataPCCV(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdTrangThaiLamViec.Title = "Xem chi tiết đổi trạng thái";
                btnSave.Hidden = true;
                hdType.Text = type;
                LoadDataPCCV(Convert.ToInt16(hdId));
            }
            wdTrangThaiLamViec.Show();
        }
        public void LoadDataPCCV(int hdId)
        {
            lst = (List<NSTrangThaiLamViec>)Session["TrangThaiLamViec"];
            NSTrangThaiLamViec item = lst.Where(a => a.TrangThaiID.Equals(hdId)).SingleOrDefault();
            dtNgayBatDau.Text = item.NgayBatDau != null ? ((DateTime)item.NgayBatDau).ToString("dd/MM/yyyy") : string.Empty;
            dtNgayKetThuc.Text = item.NgayKetThuc != null ? ((DateTime)item.NgayKetThuc).ToString("dd/MM/yyyy") : string.Empty;
            txtNoiDung.Text = item.GhiChu;
            cbbTrangThai.SelectedItem.Value = item.LoaiTrangThaiID.ToString();
        }
        protected void btnSave_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayBatDau = Convert.ToDateTime(dtNgayBatDau.Text);
            DateTime ngayKetThuc = Convert.ToDateTime(dtNgayKetThuc.Text);

            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;
            int loaiTrangThai = !string.IsNullOrEmpty(cbbTrangThai.SelectedItem.Value) ? Convert.ToInt16(cbbTrangThai.SelectedItem.Value) : 0;
            if (hdType.Text.Equals("AddNew"))
            {
                NSTrangThaiLamViec item = trangThai.GetTrangThaiLamViec(nhanVienId, loaiTrangThai, ngayBatDau, ngayKetThuc, txtNoiDung.Text);
                ((UCNhanVien)this.Parent).GetTrangThaiLamViec(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                trangThai.EditTrangThaiLamViec(Convert.ToInt16(hdTrangThaiId.Text),nhanVienId, loaiTrangThai,ngayBatDau, ngayKetThuc, txtNoiDung.Text);
                ((UCNhanVien)this.Parent).ReloadGrid("TrangThaiLamViec");
            }

            wdTrangThaiLamViec.Hide();

        }
    }
}